package cn.liumouren.jzxd.controller;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.R;
import cn.liumouren.jzxd.entity.Goods;
import cn.liumouren.jzxd.service.GoodsService;
import cn.liumouren.jzxd.utils.QueryUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author Freeman Liu
 * @email llw599502537@gmail.com
 * @date 2020-10-31 00:44:46
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @ApiOperation("万能查询")
    @RequestMapping(value = "/customQuery", method = RequestMethod.GET)
    public R getSceneNames(@RequestParam Map<String, Object> map) {
        PageUtils result = QueryUtil.getResult(goodsService, map);
        return R.ok("查询成功", result);
    }

    /**
     * 列表
     */
    @ApiOperation("分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id查询")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public R info(@PathVariable("id") String id){
		Goods goods = goodsService.getById(id);

        return R.ok().put("data", goods);
    }

    /**
     * 保存
     */
    @ApiOperation("新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@RequestBody Goods goods){
		goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public R update(@RequestBody Goods goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("批量删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestBody String[] ids){
		goodsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation("获取某个用户(商家)的所有商品")
    @RequestMapping(value = "/getAllByUserId/{userId}", method = RequestMethod.GET)
    public R getAllByUserId(@PathVariable String userId) {
        List<Goods> goodsList = goodsService.findByUserId(userId);

        return R.ok("成功查询用户[" + userId + "]的所有商品信息", goodsList);
    }

    @ApiOperation("根据关键字分页搜索")
    @RequestMapping(value = "/search/{cur}/{size}", method = RequestMethod.GET)
    public R getSceneNames(String key, @PathVariable int cur, @PathVariable int size) {
        IPage<Goods> goodss = goodsService.queryPageByKey(key, cur, size);
        return R.ok("成功根据关键字[" + key + "]查询第[" + cur + "]页(每页显示[" + size + "])商品", goodss);
    }
}
